package com.navi.loaddata.loadService.impl.teg2Data;

import com.navi.loaddata.core.errorLog.ErrorCode;
import com.navi.loaddata.core.etlException.ETLException2;
import com.navi.loaddata.core.gpEntity.WppTegStatistic2;
import com.navi.loaddata.core.gpEntity.array.WppAdefectGlassF;
import com.navi.loaddata.core.mysqlEntity.EtlConfD;
import com.navi.loaddata.core.mysqlEntity.EtlConfDModel;
import com.navi.loaddata.core.repository.impl.GreenPlumRepositoryImpl;
import com.navi.loaddata.core.util.ProcessUtil;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(transactionManager = "secondaryPlatformTransactionManager")
public class LoadTeg2InfoService {

    @Autowired
    private GreenPlumRepositoryImpl gpDao;

    public List<WppTegStatistic2> loadTeg2Info(EtlConfDModel etlConfD) throws Exception {
        Session session=gpDao.getEntityManagerSession();

        List<WppTegStatistic2> teg2List=new ArrayList<>();
        StringBuffer hql = new StringBuffer();
        hql.append("FROM WppTegStatistic2 WHERE procEnd < current_timestamp and dbTimestamp >= '")
                .append(etlConfD.getRunStartTimestamp())
                .append("' and dbTimestamp<'")
                .append(etlConfD.getRunEndTimestamp())
                .append("' and realFlg='Y' ");
            hql.append(ProcessUtil.getOpeHql(etlConfD.getOpe()));
            try{
                teg2List=gpDao.list(WppTegStatistic2.class, hql.toString());
            }catch (Exception e){
                throw new ETLException2(ErrorCode.ERROR_GET_GP_TEG2,e);
            }
        return teg2List;
    }



    private String getGlassInCondition(List<WppAdefectGlassF> wppAdefectGlassFEntityList) {
        StringBuffer glassIds = new StringBuffer();
        int size = wppAdefectGlassFEntityList.size();
        for (int i = 0; i < size; i++) {
            glassIds.append("'").append(wppAdefectGlassFEntityList.get(i).getGlassId()).append("'");
            if (i < size - 1) {
                glassIds.append(",");
            }
        }

        return glassIds.toString();
    }
}
